因子分析
spdClose
print(readLines('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_01.R'))
## [1] "################################################################################"
## [2] "## StatArb_Calendar_03_predictor_01.R"
## [3] "##"
## [4] "## 1. 计算各种 predictor"
## [5] "##"
## [6] "## predictor:"
## [7] "## spdClose,近月合约与远月合约的分钟数收盘价差"
## [8] "##"
## [9] "## Input:"
## [10] "## @dt_inSample"
## [11] "##"
## [12] "## Output:"
## [13] "## dtX"
## [14] "################################################################################"
## [15] ""
## [16] "dtX <- dt_inSample[, .(TradingDay, Minute, NumericExchTime,"
## [17] " InstrumentID.x, close.x,"
## [18] " InstrumentID.y, close.y,"
## [19] " spdClose, spdCloseMA, spdCloseSD,"
## [20] " response = spdCloseRtn)]"
## [21] "dtX[, \":=\"(predictor = (spdClose - spdCloseMA) / spdCloseSD)]"
## [22] ""
## [23] "print(dtX[, summary(predictor)])"
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_01.R')
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -7.6287007601 -0.7990617376 0.0803077829 0.0765651489 0.9408752340 9.9000000000 165
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_04_rtn.R')
##
## Call:
## lm(formula = response ~ predictor + 0, data = dtX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.006024080016 -0.000541780853 0.000068992854 0.000680885517 0.005727568451
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## predictor -0.00022318571187 0.00000406789228 -54.8652 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.00106287044 on 46243 degrees of freedom
## Multiple R-squared: 0.0611166477, Adjusted R-squared: 0.0610963445
## F-statistic: 3010.18985 on 1 and 46243 DF, p-value: < 2.220446e-16
## [1] 12.6779311406
spdCloseRank
print(readLines('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_02.R'))
## [1] "################################################################################"
## [2] "## StatArb_Calendar_03_predictor_02.R"
## [3] "##"
## [4] "## 1. 计算各种 predictor"
## [5] "##"
## [6] "## predictor"
## [7] "## spdCloseRank,近月合约与远月合约的分钟收盘价格的排序"
## [8] "##"
## [9] "## Input:"
## [10] "## @dt_inSample"
## [11] "##"
## [12] "## Output:"
## [13] "## dtX"
## [14] "################################################################################"
## [15] ""
## [16] "dtX <- dt_inSample[, .(TradingDay, Minute, NumericExchTime,"
## [17] " InstrumentID.x, close.x,"
## [18] " InstrumentID.y, close.y,"
## [19] " spdClose, spdCloseMA, spdCloseSD,"
## [20] " response = spdCloseRtn)]"
## [21] ""
## [22] "myRank <- function(x){"
## [23] " tempRes <- base::rank(x, ties.method = 'first')[which(x == x[estimatePeriod])][1]"
## [24] " return(tempRes)"
## [25] "}"
## [26] ""
## [27] "dtX[, spdCloseRank := rollapply(data = spdClose, width = estimatePeriod,"
## [28] " FUN = myRank, fill = NA, align = 'right')]"
## [29] ""
## [30] "dtX[, \":=\"(spdCloseRankMA = rollapply(data = spdCloseRank, width = estimatePeriod,"
## [31] " FUN = mean, fill = NA, align = 'right'),"
## [32] " spdCloseRankSD = rollapply(data = spdCloseRank, width = estimatePeriod,"
## [33] " FUN = sd, fill = NA, align = 'right')"
## [34] ")]"
## [35] ""
## [36] "dtX[, predictor := (spdCloseRank - spdCloseRankMA) / spdCloseRankSD]"
## [37] ""
## [38] "print(dtX[, summary(predictor)])"
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_02.R')
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -5.0325688345 -0.8890352214 -0.1776936624 0.0042827069 0.8692314215 7.1999578267 198
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_04_rtn.R')
##
## Call:
## lm(formula = response ~ predictor + 0, data = dtX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.005001892464 -0.000572778009 0.000044940097 0.000670514455 0.005143710133
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## predictor -0.00020122858977 0.00000424807911 -47.36931 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.00107439186 on 46210 degrees of freedom
## Multiple R-squared: 0.0463090496, Adjusted R-squared: 0.0462884114
## F-statistic: 2243.85183 on 1 and 46210 DF, p-value: < 2.220446e-16
## [1] 14.0433565707
spdTurnoverChgPct
print(readLines('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_03.R'))
## [1] "################################################################################"
## [2] "## StatArb_Calendar_03_predictor_03.R"
## [3] "##"
## [4] "## 1. 计算各种 predictor"
## [5] "##"
## [6] "## predictor:"
## [7] "## spdTurnoverChgPct,分钟收盘交易额的增速差"
## [8] "##"
## [9] "## Input:"
## [10] "## @dt_inSample"
## [11] "##"
## [12] "## Output:"
## [13] "## dtX"
## [14] "################################################################################"
## [15] "dtX <- dt_inSample[, .(TradingDay, Minute, NumericExchTime,"
## [16] " InstrumentID.x, close.x,"
## [17] " InstrumentID.y, close.y,"
## [18] " turnover.x, turnover.y,"
## [19] " response = spdCloseRtn)]"
## [20] ""
## [21] "dtX[, \":=\"(turnoverChgPct.x = rollapply(turnover.x, estimatePeriod,"
## [22] " FUN = function(x){(x[length(x)] - x[1]) / x[1]},"
## [23] " fill = NA, align = 'right'),"
## [24] " turnoverChgPct.y = rollapply(turnover.y, estimatePeriod,"
## [25] " FUN = function(x){(x[length(x)] - x[1]) / x[1]},"
## [26] " fill = NA, align = 'right')"
## [27] ")]"
## [28] ""
## [29] "dtX[, spdTurnoverChgPct := turnoverChgPct.y - turnoverChgPct.x]"
## [30] "dtX[, predictor := rollapply(data = spdTurnoverChgPct, width = estimatePeriod,"
## [31] " FUN = function(x){return((x[length(x)] - mean(x)) / sd(x))},"
## [32] " fill = NA, align = 'right')]"
## [33] "print(dtX[, summary(predictor)])"
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_03.R')
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -9.8833422601 -0.0449373171 0.1395350533 -0.0177922656 0.2582391452 9.8984403196 198
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_04_rtn.R')
##
## Call:
## lm(formula = response ~ predictor + 0, data = dtX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.004975151112 -0.000594665076 0.000000218699 0.000633233432 0.004982178023
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## predictor -0.00000119647633 0.00000452294458 -0.26453 0.79137
##
## Residual standard error: 0.00110016684 on 46210 degrees of freedom
## Multiple R-squared: 1.51435977e-06, Adjusted R-squared: -2.01259451e-05
## F-statistic: 0.0699786708 on 1 and 46210 DF, p-value: 0.791369018
## [1] -3.35316846988
cor(spdCloseZ, spdTurnoverChgPctZ)
print(readLines('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_04.R'))
## [1] "################################################################################"
## [2] "## StatArb_Calendar_03_predictor_04.R"
## [3] "##"
## [4] "## 1. 计算各种 predictor"
## [5] "##"
## [6] "## predictor:"
## [7] "## cor(spdCloseMA, spdTurnoverChgPct)"
## [8] "##"
## [9] "## Input:"
## [10] "## @dt_inSample"
## [11] "##"
## [12] "## Output:"
## [13] "## dtX"
## [14] "################################################################################"
## [15] "dtX <- dt_inSample[, .(TradingDay, Minute, NumericExchTime,"
## [16] " InstrumentID.x, close.x,"
## [17] " InstrumentID.y, close.y,"
## [18] " turnover.x, turnover.y,"
## [19] " spdCloseZ = (spdClose - spdCloseMA)/spdCloseSD,"
## [20] " response = spdCloseRtn)]"
## [21] ""
## [22] "dtX[, \":=\"(turnoverChgPct.x = rollapply(turnover.x, estimatePeriod,"
## [23] " FUN = function(x){(x[length(x)] - x[1]) / x[1]},"
## [24] " fill = NA, align = 'right'),"
## [25] " turnoverChgPct.y = rollapply(turnover.y, estimatePeriod,"
## [26] " FUN = function(x){(x[length(x)] - x[1]) / x[1]},"
## [27] " fill = NA, align = 'right')"
## [28] ")]"
## [29] ""
## [30] "dtX[, spdTurnoverChgPct := turnoverChgPct.y - turnoverChgPct.x]"
## [31] "dtX[, spdTurnoverChgPctZ := rollapply(data = spdTurnoverChgPct, width = estimatePeriod,"
## [32] " FUN = function(x){return((x[length(x)] - mean(x)) / sd(x))},"
## [33] " fill = NA, align = 'right')]"
## [34] "# dtX <- dtX[!is.na(spdCloseZ)][!is.na(spdTurnoverChgPctZ)]"
## [35] "dtX[, predictor := rollapply(data = 1:.N, width = estimatePeriod,"
## [36] " FUN = function(i){"
## [37] " cor(dtX[i]$spdCloseZ,dtX[i]$spdTurnoverChgPctZ)"
## [38] " }, fill = NA, align = 'right')]"
## [39] ""
## [40] "print(dtX[, summary(predictor)])"
## [41] ""
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_04.R')
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -0.562941321 -0.086704516 0.006509520 0.006452175 0.100335299 0.720445164 462
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_04_rtn.R')
##
## Call:
## lm(formula = response ~ predictor + 0, data = dtX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.004914938644 -0.000596161100 0.000002720356 0.000632608531 0.004945038928
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## predictor 0.0001497032008 0.0000346397854 4.32171 0.000015515 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.00109634758 on 45946 degrees of freedom
## Multiple R-squared: 0.000406338126, Adjusted R-squared: 0.000384582289
## F-statistic: 18.6772008 on 1 and 45946 DF, p-value: 0.0000155147697
## [1] 1.1142730719
cor(highChgPct.x, highChgPct.y)
print(readLines('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_05.R'))
## [1] "################################################################################"
## [2] "## StatArb_Calendar_03_predictor_05.R"
## [3] "##"
## [4] "## 1. 计算各种 predictor"
## [5] "##"
## [6] "## predictor:"
## [7] "## cor(highChgPct.x, highChgPct.y)"
## [8] "##"
## [9] "## Input:"
## [10] "## @dt_inSample"
## [11] "##"
## [12] "## Output:"
## [13] "## dtX"
## [14] "################################################################################"
## [15] "dtX <- dt_inSample[, .(TradingDay, Minute, NumericExchTime,"
## [16] " InstrumentID.x, close.x,"
## [17] " InstrumentID.y, close.y,"
## [18] " high.x, high.y,"
## [19] " response = spdCloseRtn)]"
## [20] ""
## [21] "dtX[, \":=\"("
## [22] " highChgPct.x = rollapply(data = high.x, width = estimatePeriod,"
## [23] " FUN = function(x){"
## [24] " (x[length(x)] - x[1]) / x[length(x)]},"
## [25] " fill = NA, align = 'right'),"
## [26] " highChgPct.y = rollapply(data = high.y, width = estimatePeriod,"
## [27] " FUN = function(x){"
## [28] " (x[length(x)] - x[1]) / x[length(x)]},"
## [29] " fill = NA, align = 'right')"
## [30] ")]"
## [31] ""
## [32] "dtX[, predictor := rollApply(data = .SD[, .(highChgPct.x, highChgPct.y)], window = 3,"
## [33] " fun = function(x){"
## [34] " # print(x)"
## [35] " cor(x$highChgPct.x, x$highChgPct.y)"
## [36] " }, align = 'right'"
## [37] ")]"
## [38] ""
## [39] "print(dtX[, summary(predictor)])"
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_05.R')
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -1.00000000 0.50925579 0.94357693 0.70512718 0.99790692 1.00000000 4927
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_04_rtn.R')
##
## Call:
## lm(formula = response ~ predictor + 0, data = dtX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.005029540247 -0.000634007005 -0.000049517065 0.000600327798 0.004941041348
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## predictor 0.00005717738204 0.00000625313266 9.1438 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.00109565415 on 41481 degrees of freedom
## Multiple R-squared: 0.00201154406, Adjusted R-squared: 0.00198748513
## F-statistic: 83.6090423 on 1 and 41481 DF, p-value: < 2.220446e-16
## [1] 3.15626675475
|high - low|
print(readLines('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_06.R'))
## [1] "################################################################################"
## [2] "## StatArb_Calendar_03_predictor_06.R"
## [3] "##"
## [4] "## 1. 计算各种 predictor"
## [5] "##"
## [6] "## predictor:"
## [7] "##"
## [8] "##"
## [9] "## Input:"
## [10] "## @dt_inSample"
## [11] "##"
## [12] "## Output:"
## [13] "## dtX"
## [14] "################################################################################"
## [15] "dtX <- dt_inSample[, .(TradingDay, Minute, NumericExchTime,"
## [16] " InstrumentID.x, close.x,"
## [17] " InstrumentID.y, close.y,"
## [18] " high.x, low.x, high.y, low.y,"
## [19] " response = spdCloseRtn)]"
## [20] ""
## [21] "dtX[, HLRatio := (high.x - low.x) / (high.y - low.y)]"
## [22] ""
## [23] "dtX[, predictor := rollapply(data = HLRatio, width = estimatePeriod,"
## [24] " FUN = function(x){"
## [25] " (x[length(x)] - mean(x)) / sd(x)"
## [26] " }, fill = NA, align = 'right'"
## [27] ")]"
## [28] ""
## [29] "print(dtX[, summary(predictor)])"
## [30] ""
## [31] ""
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_03_predictor_06.R')
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -3.45066233 -0.65886875 0.12902086 0.00297361 0.53942968 6.36464367 12381
source('/home/william/Documents/myStrat/projects/StatArb/R/StatArb_Calendar_04_rtn.R')
##
## Call:
## lm(formula = response ~ predictor + 0, data = dtX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.004786514699 -0.000599968831 0.000003370197 0.000638614794 0.004831569379
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## predictor -0.00000860497344 0.00000591697135 -1.45429 0.14588
##
## Residual standard error: 0.00109347916 on 34027 degrees of freedom
## Multiple R-squared: 6.21511947e-05, Adjusted R-squared: 3.27645944e-05
## F-statistic: 2.11495015 on 1 and 34027 DF, p-value: 0.145876027
## [1] 0.697277389326